﻿ using System.ComponentModel;
using System;
 using System.Globalization;
 using System.Linq;
 using System.Xml.Serialization;

namespace NTTDATA.BEENChina.ResultCertification.PropertyGridUtil.CustomObject
{
    [TypeConverter(typeof(CustomConverter))]
    public class SpecialStringArray  : PropertyData
    {

        private int _row = -1, _col = -1 ;
         

        [Category("range")]
        public int Row { get { return _row; } set { _row = value; } }
        [Category("range")]
        public int Col { get { return _col; } set { _col = value; } }
        [Category("range")]
        public int Group{ get; set; }
         

        public EnumSpecialType SpecialType { get; set; }

        [XmlArray(ElementName = "SpecialArray", IsNullable = true)]
        [XmlArrayItem(ElementName = "SpecialString", NestingLevel = 0, IsNullable = true)]
        public String[] SpecialArray
        {
            get; 
            set; 
        }

        #region PropertyData impl
        public string GetDisplayName()
        {
            return SpecialArray == null ? "" : SpecialType.ToString() + ":" + string.Join(",", SpecialArray);
        }
        public string GetDescription()
        {
            return SpecialArray == null ? "" : SpecialType.ToString() + ":" + string.Join(",", SpecialArray);
        }

        public string GetValue()
        {
            return SpecialArray == null ? "" : SpecialType.ToString() + ":" + string.Join(",", SpecialArray);
        }
        #endregion


        public string ErrorRanage { get; set; }

        public bool Check(string result)
        {
            bool bresult = false;

            if (SpecialType == EnumSpecialType.Include)
            {
                bresult = SpecialArray.Any(
                    t=> {
                        bool bCheck = false;
                        bCheck = result.Contains(t);

                        if (bCheck)
                        {
                            ErrorRanage = t;
                        }
                       return  bCheck;

                });
            }
            else if (SpecialType == EnumSpecialType.StartWith)
            {
                bresult = SpecialArray.Any(result.StartsWith);
            }
            else if (SpecialType == EnumSpecialType.EndWith)
            {
                bresult = SpecialArray.Any(result.EndsWith);
            }
            else if (SpecialType == EnumSpecialType.Equal)
            {
                bresult = SpecialArray.Any(result.Equals);
            }
            else if (SpecialType == EnumSpecialType.UnInclude)
            {
                bresult = SpecialArray.All(s => !result.Contains(s));
            }
            else if (SpecialType == EnumSpecialType.UnEqual)
            {
                bresult = SpecialArray.All(s => !result.Equals(s));
            }
            else if (SpecialType == EnumSpecialType.EveryCharUnEqual)
            {
               //每个字符比较，出现字符不在列表里 即认为错误
              return  result.ToCharArray().Any(t => !SpecialArray.Contains(t.ToString(CultureInfo.InvariantCulture)));
           
            }
            else if (SpecialType == EnumSpecialType.EveryCharEqual)
            {
                return result.ToCharArray().Any(t =>
                                                    {
                                                        bool bCheck = false;
                                                        bCheck = SpecialArray.Contains(t.ToString(CultureInfo.InvariantCulture));

                                                        if (bCheck)
                                                        {
                                                           ErrorRanage = SpecialArray.Single(tt =>
                                                                tt.Contains(t.ToString(CultureInfo.InvariantCulture)));
                                                        }
                                                        return bCheck;
                                                    });
            }
            else if (SpecialType == EnumSpecialType.UnEndWith)
            {
                bresult = SpecialArray.Any( t=>! result.EndsWith(t));
            }
            return bresult;
        }

        public bool Check(int row,int col,string result)
        {
            if (_row.Equals(-1) && _col.Equals(-1))
            {
                return Check(result);
            }

            if (_row.Equals(-1) && col.Equals(_col))
            {
                return Check(result);
            }

            if (row.Equals(_row) && _col.Equals(-1))
            {
                return Check(result);
            }

            if (row.Equals(_row) && col.Equals(_col))
            {
                return Check(result);
            }

            return false;
        }
    }
   
}
